package cn.yfj.org.controller;

import cn.yfj.basic.util.AjaxResult;
import cn.yfj.basic.util.PageList;
import cn.yfj.org.domain.Detail;
import cn.yfj.org.query.DetailQuery;
import cn.yfj.org.service.IDetailServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //@Controller+@responsebody
@RequestMapping("/detail")
public class DetailController {
    @Autowired
    private IDetailServiceI detailService;

    //增加和修改
    @PutMapping
    public AjaxResult remove(@RequestBody Detail detail){
        try {
            //如果id为空则添加
            if(detail.getId()==null){
                detailService.save(detail);
            }else {
                //否则就调用修改方法
                detailService.update(detail);
            }
            //返回操作正确信息
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            //返回错误信息
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    //删除
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id){
        try {
            detailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


    //批量删除
    @PatchMapping
    public AjaxResult remove(@RequestBody List<Long> ids){
        try {
            detailService.bach(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    //查询一个
    @GetMapping("/{id}")
    public Detail queryById(@PathVariable Long id){
        return detailService.queryById(id);
    }

    //查询所有
    @GetMapping
    public List<Detail> queryAll(){
        return detailService.queryAll();
    }

    //分页和高级查询
    @PostMapping
    public PageList<Detail> queryPage(@RequestBody DetailQuery detailQuery){
        return detailService.queryPage(detailQuery);
    }




}